package alva.com.getcamera.view.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import alva.com.getcamera.R;
import alva.com.getcamera.adapter.PicRecyclerViewAdapter;
import alva.com.getcamera.bean.ImageBean;
import alva.com.getcamera.customview.MyDividerDecoration;
import alva.com.getcamera.utils.FileOperateUtil;
import alva.com.getcamera.view.activity.PicActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class PicFragment extends Fragment implements PicRecyclerViewAdapter.OnChildClickListener, SwipeRefreshLayout.OnRefreshListener, PicRecyclerViewAdapter.OnChildLongClickListener {

    private RecyclerView picRecyclerView;
    private SwipeRefreshLayout refreshLayout;
    private ArrayList<ImageBean> imageList = new ArrayList<>();
    private PicRecyclerViewAdapter adapter;

    private final static String picDirectory = "/storage/emulated/0/myPic";
    private final static String picFormat = ".jpg";

    public PicFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pic, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        picRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewPic);
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
        adapter = new PicRecyclerViewAdapter(getContext(), imageList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        picRecyclerView.setLayoutManager(gridLayoutManager);
        picRecyclerView.addItemDecoration(new MyDividerDecoration(getContext(), MyDividerDecoration.VERTICAL_LIST, R.drawable.decration_shape));
        picRecyclerView.setItemAnimator(new DefaultItemAnimator());
        picRecyclerView.setAdapter(adapter);

        adapter.setOnChildClickListener(this);
        adapter.setOnChildLongClickListener(this);
        refreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorGreen, R.color.colorPrimary, R.color.colorYellow);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter.clear();
        imageList.clear();
        imageList= FileOperateUtil.getPictureList(picDirectory,picFormat);
        adapter.addAll(imageList);
    }

    @Override
    public void onPause() {
        super.onPause();
        adapter.clear();
        imageList.clear();
    }

    @Override
    public void onChildClick(RecyclerView parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), PicActivity.class);
        intent.putExtra("position", position);
        intent.putParcelableArrayListExtra("imageList", imageList);
        startActivity(intent);
    }

    @Override
    public void onChildLongClick(RecyclerView parent, View view, final int position, long id, final String filePath) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("删除图片");
        builder.setMessage("确认删除这张图片?");
        builder.setCancelable(true);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                adapter.remove(position);
                FileOperateUtil.remove(filePath);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onRefresh() {
        adapter.clear();
        imageList= FileOperateUtil.getPictureList(picDirectory,picFormat);
        adapter.addAll(imageList);
        refreshLayout.setRefreshing(false);
    }
}
